/**
 * 
 */
package nus.mtech.presentation;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;

import nus.mtech.ApplicationFacade;
import nus.mtech.constant.PresentationConstant;
import nus.mtech.user.User;

/**
 * @author Alvin Lee
 * 
 */
final class LoginPanel extends JPanel implements ActionListener {
	private static final long serialVersionUID = 864551054730337325L;
	private Dimension screen = null;
	private static volatile LoginPanel INSTANCE;
	private JTextField loginTextField;
	private JPasswordField passwordField;
	private JButton btnLogin;
	private JButton btnClear;

	LoginPanel() {
		super(null);
		this.setBackground(PresentationConstant.CONSOLE_BACKGROUND);
		this.setPreferredSize(new Dimension(1004, 643));
		initialization();
	}

	/**
	 * Creates Batch Console
	 * 
	 * @param user
	 *            UserLogin
	 */
	public void initialization() {
		try {
			removeAll();

			screen = Toolkit.getDefaultToolkit().getScreenSize();

			final JLabel importDataPanelLabel = new JLabel();
			importDataPanelLabel.setHorizontalAlignment(SwingConstants.CENTER);
			importDataPanelLabel.setFont(new Font("", Font.BOLD, 16));
			importDataPanelLabel.setText("Login");
			importDataPanelLabel.setBounds(0, 10, 1004, 21);
			add(importDataPanelLabel);

			JLabel borderBase = new JLabel();
			borderBase.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
			borderBase.setBounds(40, 71, 944, 342);
			add(borderBase);

			JLabel lblLoginId = new JLabel("Login ID:");
			lblLoginId.setBounds(105, 133, 106, 14);
			add(lblLoginId);

			loginTextField = new JTextField();
			loginTextField.setBounds(170, 130, 316, 20);
			add(loginTextField);
			loginTextField.setColumns(10);

			JLabel lblPassword = new JLabel("Password:");
			lblPassword.setBounds(105, 185, 106, 14);
			add(lblPassword);

			passwordField = new JPasswordField();
			passwordField.setBounds(170, 182, 316, 20);
			add(passwordField);

			btnLogin = new JButton("Login");
			btnLogin.setBounds(287, 331, 89, 23);
			btnLogin.addActionListener(this);
			add(btnLogin);

			btnClear = new JButton("Clear");
			btnClear.setBounds(463, 331, 89, 23);
			btnClear.addActionListener(this);
			add(btnClear);

		} catch (Exception ex) {
			System.out.println("exception");
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent ae) {
		if (ae.getSource() == btnLogin) {
			//perform Login Actions
			if (loginTextField.getText().length() == 0 || passwordField.getText().length() == 0) {
				JOptionPane.showMessageDialog(this,
                        "Login ID Or Password Required.", "Please enter Login ID and password",
                        JOptionPane.ERROR_MESSAGE);
			}
			else {
				User user = ApplicationFacade.getInstance().login(loginTextField.getText(), passwordField.getText());
				if (user != null) {
					Console.setLoginUser(user);
					Console.enableAllPanes(true);
					ViewBillPanel.getInstance().setAdminFunction(user.isAdmin());
					CustomerPanel.getInstance().setAdminFunction(user.isAdmin());
					//Disable/Enable Subscription pane
					Console.togglePaneEnabler(2, user.isAdmin());
					clear();
				}
				else {
					JOptionPane.showMessageDialog(this,
	                        "Invalid Login ID or Password.", "Invalid Login ID or Password.",
	                        JOptionPane.ERROR_MESSAGE);
				}
			}
		}
		else if (ae.getSource() == btnClear) {
			clear();
		}
	}
	
	
	public void clear() {
		loginTextField.setText("");
		passwordField.setText("");
	}
	
	public static LoginPanel getInstance() {
		synchronized (LoginPanel.class) {
			if (INSTANCE == null) {
				INSTANCE = new LoginPanel();
			}
			return INSTANCE;
		}
	}
}
